﻿using NConf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using NConf.Interfaces;
using NConf.ConfigSources;
using System.Collections.Generic;

namespace NConfTests
{
    [TestClass()]
    public class ConfigKeyTest
    {

        ConfigManager confman;

        #region Test Context

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #endregion

        #region Additional test attributes

        // Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void TestInitialize()
        {
            confman = new ConfigManager();

            IConfigSource source1 = new DictConfigSource();
            source1.Configuration["test"] = new Dictionary<string, object> { { "foo", "bar" }, { "sub", new Dictionary<string, object> { { "dom", true } } } };
            source1.Configuration["bar"] = "baz";
            source1.Configuration["cats"] = "apples";

            IConfigSource source2 = new DictConfigSource();
            source2.Configuration["test"] = "foo";
            source2.Configuration["bar"] = "foo";
            source2.Configuration["dogs"] = "oranges";

            IConfigSource source3 = new DictConfigSource();
            source3.Configuration["test"] = "foo";
            source3.Configuration["xyz"] = "abc";
            source3.Configuration["dogs"] = "apples";

            confman.AddConfigSource(source1);
            confman.AddConfigSource(source3);
            confman.AddConfigSource(source2);
        } // end TestInitialize

        #endregion


        [TestMethod()]
        public void HasValueTest()
        {
            var dogsKey = confman.GetConfigKey("dogs");
            var testKey = confman.GetConfigKey("test");
            var novalKey = confman.GetConfigKey("iuwue");

            Assert.IsTrue(dogsKey.HasValue());
            Assert.IsTrue(testKey.HasValue());
            Assert.IsFalse(novalKey.HasValue());
        } // end HasValueTest

        [TestMethod()]
        public void GetValueTest()
        {
            dynamic dogsKey = confman.GetConfigKey("dogs");
            dynamic testKey = confman.GetConfigKey("test");

            Assert.AreEqual("apples", dogsKey.GetValue());
            Assert.AreEqual("bar", testKey.foo);
        } // end GetValueTest

        [TestMethod()]
        public void NonExistantMemberTest()
        {
            dynamic dogsKey = confman.GetConfigKey("dogs.lassie");
            dynamic testKey = confman.GetConfigKey("doesnotexist");

            Assert.AreEqual("bark", dogsKey.GetValue("bark"));
            Assert.AreEqual("bleh", testKey.foo.GetValue("bleh"));
        } // end GetValueTest

        [TestMethod()]
        public void IsSectionTest()
        {
            var dogsKey = confman.GetConfigKey("dogs");
            var testKey = confman.GetConfigKey("test");

            Assert.IsFalse(dogsKey.IsSection());
            Assert.IsTrue(testKey.IsSection());
        } // end IsSectionTest

        [TestMethod()]
        public void ToStringTest()
        {
            dynamic dogsKey = confman.GetConfigKey("dogs");

            Assert.AreEqual("apples", dogsKey.ToString());
        } // end ToStringTest

        [TestMethod()]
        public void DynamicTest()
        {
            dynamic dogsKey = confman.GetConfigKey("dogs");
            dynamic testKey = confman.GetConfigKey("test");

            Assert.AreEqual("apples", dogsKey);
            Assert.IsTrue(testKey.sub.dom);
        } // end GetValueTest

    } // end ConfigKeyTest
} // end NConfTests namespace
